<template>
  <HomePanel title="新鲜好物" sub-title="新鲜出炉 品质靠谱" ref="target">
    <template #right>
      <XtxMore></XtxMore>
    </template>
    <template #default v-if="list.length > 0">
      <ul class="goods-list">
        <li v-for="item in list" :key="item.id">
          <RouterLink to="/">
            <img v-imgLazy="item.picture" alt="" />
            <p class="name">{{ item.name }}</p>
            <p class="price">&yen;{{ item.price }}</p>
          </RouterLink>
        </li>
      </ul>
    </template>
    <!-- v-if 这儿有 #default / v-else 也要指定 #default -->
    <template #default v-else>
      <ul class="goods-list">
        <li v-for="item in 4" :key="item">
          <XtxSkeleton
            :width="306"
            :height="406"
            bg="rgba(0,0,0,0.6)"
          ></XtxSkeleton>
        </li>
      </ul>
    </template>
  </HomePanel>
</template>

<script>
import { reqFindNew } from '@/api/home'
import HomePanel from './home-panel.vue'
import { ref } from 'vue-demi'
import { useObserver } from '@/compositions'
export default {
  name: 'HomeNew',
  components: {
    HomePanel
  },
  setup() {
    const list = ref([])
    // !获取数据并渲染
    const getList = async () => {
      const res = await reqFindNew()
      list.value = res.result
    }
    // 进入可视区，调用 getList
    const { target } = useObserver(getList)
    return { list, target }
  }
}
</script>
<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
